<?php
/**
 * Created by PhpStorm.
 * User: Qch
 * Date: 2017/11/24
 * Time: 6:07
 */

namespace Application\System\Controller;


use J\Intent;
use J\MVC\Controller;
use J\Util\Verify;

class VerifyController extends Controller
{
    // 返回验证码
    protected function optionApiIndex()
    {
        $verify = new Verify(['imageW' => 300]);
        $verify->entry($this->getKey());
        die();
    }

    protected function getKey()
    {
        $app = $this->getApp();
        return $app->get('ApplicationKey') . $app->get('LoginSession', 'LoginSession');
    }

    protected function optionApiCheck(Intent $intent)
    {
        if (
            isset($_REQUEST['value'])
            and
            $this->checkVerify($_REQUEST['value'], false)

        ) {
            $intent->ajax(true);
        } else {
            $intent->ajax(false, '验证码错误');
        }
    }

    public function checkVerify($value, $expire = true)
    {
        if (empty($value)) return false;

        $verify = new Verify();
        return $verify->check(
            $value,
            $this->getKey(),
            $expire
        );
    }

    protected function optionFetchCheck(Intent $intent)
    {
        $verify = $intent->get('verify');
        if (
        $this->checkVerify($verify)
        ) {
            return $intent->value = true;
        }
        return $intent->value = false;
    }
}